package com.example.yaoqiang.smsdemo;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

public class MySMSManager extends AppCompatActivity implements View.OnClickListener {

    private TextView tvUnread;
    private ContentResolver cr;
    private TextView tvSms;
    private TextSpeaker mTextSpeaker;
    private String smsNumber;
    private String smsBody;
    private String smsName;
    private int unreadNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_smsmanager);

        mTextSpeaker = new TextSpeaker(this);
        cr = this.getContentResolver();

        tvUnread = (TextView) findViewById(R.id.tvSMS);
        tvSms = (TextView) findViewById(R.id.tvBody);
        findViewById(R.id.btnRead).setOnClickListener(this);



    }

    public void readShortMessage() {
        cr = this.getContentResolver();
        Cursor cursor = cr.query(Uri.parse("content://sms/inbox"), new String[]{"_id",
                        "address", "thread_id", "date", "protocol", "type", "body",
                        "read" }, // 返回的列
               null, // 查询条件
                null,// 匹配条件
                "date desc"); // 降序排列

//        while (cursor.moveToNext()) {
        cursor.moveToNext();
//            int read = cursor.getColumnIndex("read");
            //未读
//            if (cursor.getString(read).endsWith("0")) {
                int phoneColumn = cursor.getColumnIndex("address");
                int smsColumn = cursor.getColumnIndex("body");

                smsNumber = cursor.getString(phoneColumn);
                smsBody = cursor.getString(smsColumn);
                smsName = getContactName(smsNumber);

        tvSms.setText(smsNumber+" "+smsName+" "+smsBody);

        if (smsName != null) {
            mTextSpeaker.speak("收到短信，来自"+smsName+"。"+smsBody);
        }else {
            //号码直接朗读会是一个很大的数字
            //所以在号码中间加入空格
            String tmp = null;

            for (int i=0; i<smsNumber.length(); i++) {
                tmp += smsNumber.charAt(i)+"，";
            }

            mTextSpeaker.speak("收到短信，来自"+tmp+"。"+smsBody);
        }

//                unreadNum ++;


//            }
//        }

//        tvUnread.setText(String.valueOf(unreadNum));
    }

    private String getContactName(String number) {
        if (TextUtils.isEmpty(number)) {
            return null;
        }

        final ContentResolver resolver = this.getContentResolver();

        Uri lookupUri = null;
        String[] projection = new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME };
        Cursor cursor = null;
        try {
            lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
            cursor =resolver.query(lookupUri, projection, null, null, null);
        } catch (Exception ex) {
            ex.printStackTrace();
            try {
                lookupUri = Uri.withAppendedPath(android.provider.Contacts.Phones.CONTENT_FILTER_URL,
                        Uri.encode(number));
                cursor = resolver.query(lookupUri, projection, null, null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        String ret = null;
        if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
            ret = cursor.getString(1);
        }

        cursor.close();
        return ret;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnRead:
                readShortMessage();
                break;
        }
    }
}
